package com.xbongbong.paas.help.team;

import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.saas.constant.StringConstant;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author shen.yang
 * @version v1.0
 * @Description 团队成员redis工具类
 * @date 2022/1/11 14:00
 * @since v1.0
 */
@Component
public class TeamRedisHelper {
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
    * redis中团队成员集合的size最大值
    */
    private final static Long MAX_SIZE = 15L;

    /**
     * @Description: 添加常用团队成员
     * @param corpId 公司id
     * @param userId 操作人id
     * @param memberIds 常用成员id
     * @param time 添加时间戳，用于zset排序
     * @return: java.lang.Long
     * @Author: shen.yang
     * @Date: 2022/1/11  11:10
     */
    public void addRecentlyMember(String corpId, String userId, List<String> memberIds, Long time){
        if(CollectionUtils.isEmpty(memberIds)){
            return;
        }
        String key = corpId + StringConstant.UNDER_LINE + userId;
        HashSet<Object> values = new HashSet<>();
        //向集合中插入多个元素
        for (String member : memberIds) {
            DefaultTypedTuple<String> tuple = new DefaultTypedTuple(member, time.doubleValue());
            values.add(tuple);
        }
        paasRedisHelper.addZsetValue(RedisPrefixConstant.TEAM_MEMBER_PREFIX, key, values);
    }


    /**
     * @Description: 查询常用团队成员size
     * @param corpId 公司id
     * @param userId 操作人id
     * @return: java.lang.Long
     * @Author: shen.yang
     * @Date: 2022/1/11  11:16
     */
    public Long recentlyMemberSize(String corpId, String userId){
        String key = corpId + StringConstant.UNDER_LINE + userId;
        return paasRedisHelper.zsetSize(RedisPrefixConstant.TEAM_MEMBER_PREFIX,key);
    }

    /**
     * @Description: 获取常用团队成员列表（按照时间戳从大到小，时间戳最大的index=0）
     * @param corpId 公司id
     * @param userId 操作人id
     * @return: java.util.Set
     * @Author: shen.yang
     * @Date: 2022/1/11  11:18
     */
    public Set<String> getRecentlyMembers(String corpId, String userId){
        String key = corpId + StringConstant.UNDER_LINE + userId;
        return paasRedisHelper.getZset(RedisPrefixConstant.TEAM_MEMBER_PREFIX,key,0L, MAX_SIZE - 1);
    }

    /**
     * @Description: 删除zset中多余的members
     * @param corpId 公司id
     * @param userId 操作人id
     * @return: java.lang.Long
     * @Author: shen.yang
     * @Date: 2022/1/11  14:07
     */
    public void removeExtraMembers(String corpId, String userId){
        // 拼装实际key
        String key = corpId + StringConstant.UNDER_LINE + userId;
        Long size = paasRedisHelper.zsetSize(RedisPrefixConstant.TEAM_MEMBER_PREFIX, key);
        if(size <= MAX_SIZE){
            return;
        }
        long end = size - MAX_SIZE - 1;
        paasRedisHelper.removeZsetValueByIndex(RedisPrefixConstant.TEAM_MEMBER_PREFIX,key,0L,end);
    }
}
